package com.itheima.service.impl;

import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.entity.TemplateExportParams;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.mapper.ReportMapper;
import com.itheima.pojo.Order;
import com.itheima.pojo.Setmeal;
import com.itheima.res.ExcelRes;
import com.itheima.service.ReportService;
import com.itheima.utils.DateUtil;
import org.apache.dubbo.config.annotation.Service;
import org.apache.poi.ss.usermodel.Workbook;

import javax.annotation.Resource;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.NumberFormat;
import java.util.*;


@Service
public class ReportServiceImpl implements ReportService {

    @Resource
    private ReportMapper reportMapper;


    @Override
    public Map findOperationalData() {

        Map map = new HashMap();

        //会员统计
        Integer curmonthmemebers = reportMapper.findMemberByMonth();
        Integer curweekmember = reportMapper.findMemberByWeek();
        Integer totalmemebers = reportMapper.findMemberCount();
        Integer curdatemember = reportMapper.findMemberByDay();

        //预约统计
        Integer orderCount = reportMapper.findOrder();
        Integer curmonthordersettings = reportMapper.findOrderByMonth();
        Integer curweekordersettings = reportMapper.findOrderByWeek();
        Integer curordersettings = reportMapper.findOrderByDay();


        //预约到诊统计
        Integer curmonthorderedsettings = reportMapper.findOrderExamineByMonth();
        Integer curweekorderedsettings = reportMapper.findOrderExamineByWeek();
        Integer curorderedsettings = reportMapper.findOrderExamineByDay();


        ArrayList<Map> hotMealList = new ArrayList<>();
        //热门套餐
        List<Setmeal> mealList = reportMapper.findMeal();
        // 创建一个数值格式化对象
        NumberFormat numberFormat = NumberFormat.getInstance();
        // 设置精确到小数点后2位
        numberFormat.setMaximumFractionDigits(2);
        for (Setmeal setmeal : mealList) {
            Long setmealId = setmeal.getId();
            Integer count = reportMapper.findOrderByMealId(setmealId);
            if (count > 5) {
                Map orderMap = new HashMap();
                String result = numberFormat.format(((float) count / (float) orderCount) * 100);
                orderMap.put("setmeal_count", count);
                orderMap.put("name", setmeal.getName());
                orderMap.put("remark", setmeal.getRemark());
                orderMap.put("proportion", result + "%");
                hotMealList.add(orderMap);
            }
        }
        //热门套餐
        map.put("hotSetmeal", hotMealList);
        //会员
        map.put("thisMonthNewMember", curmonthmemebers);
        map.put("thisWeekNewMember", curweekmember);
        map.put("totalMember", totalmemebers);
        map.put("todayNewMember", curdatemember);
        //预约到诊统计
        map.put("thisMonthOrderNumber", curmonthorderedsettings);
        map.put("thisWeekOrderNumber", curweekorderedsettings);
        map.put("todayOrderNumber", curorderedsettings);
        //预约统计
        map.put("todayVisitsNumber", curordersettings);
        map.put("thisWeekVisitsNumber", curweekordersettings);
        map.put("thisMonthVisitsNumber", curmonthordersettings);

        map.put("reportDate", DateUtil.formatDate(new Date()));
        return map;
    }

    @Override
    public boolean createExcel(ExcelRes excelRes) {
        TemplateExportParams params = new TemplateExportParams("report_template.xlsx");
        Map data = new HashMap<>();
        data.put("curdate",excelRes.getReportDate());
        data.put("curdatemember", excelRes.getTodayNewMember());
//        data.put("totalmemebers", excelRes.getTotalMember());
//        data.put("curweekmember", excelRes.getThisWeekNewMember());
//        data.put("curmonthmemebers", excelRes.getThisMonthNewMember());
//        data.put("curordersettings", excelRes.getTodayOrderNumber());
//        data.put("curorderedsettings", excelRes.getTodayVisitsNumber());
//        data.put("curweekordersettings", excelRes.getThisWeekOrderNumber());
//        data.put("curweekorderedsettings", excelRes.getThisWeekVisitsNumber());
//        data.put("curmonthordersettings", excelRes.getThisMonthOrderNumber());
//        data.put("curmonthorderedsettings", excelRes.getThisMonthVisitsNumber());
//        List<Map<String, String>> setmealList = excelRes.getHotSetmeal();
//        List<Map<String, String>> listMap = new ArrayList<Map<String, String>>();
//        for (int i = 0; i < setmealList.size(); i++) {
//            Map<String, String> lm = new HashMap<String, String>();
//            lm.put("t.name", setmealList.get(i).get("name"));
//            lm.put("t.counts", setmealList.get(i).get("setmeal_count"));
//            lm.put("t.zhanbi", setmealList.get(i).get("proportion"));
//            lm.put("t.remark", setmealList.get(i).get("remark"));
//            listMap.add(lm);
//        }
//        data.put("hotSetmeal", setmealList);setmealList
            Workbook workbook = ExcelExportUtil.exportExcel(params, data);

        try {
            FileOutputStream fos = new FileOutputStream("D:/report_template.xlsx");
            workbook.write(fos);
            fos.flush();
            fos.close();
        } catch (IOException e) {
            e.printStackTrace();
        }


        return true;
    }
}
